<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<#assign len = domain.className?index_of("DO")>
<#assign complete_package_class_name="${domain.packageName}.${domain.className}">
<#assign resultMap=domain.className+"Result">
<#assign primaryProperty="{${tableDO.primaryProperty}}">

<mapper namespace="${domain.daoPackageName}.${domain.className}Mapper">
	
	<resultMap type="${complete_package_class_name}" id="${resultMap}">
	<#if tableDO.primaryProperty?exists>
		<id column="${tableDO.primaryKey}" property="${tableDO.primaryProperty}"/>
	</#if>
	<#list tableDO.columnList as col>
		<#if col.name!=tableDO.primaryKey>
		<result column="${col.name}" property="${col.propertyName}"/>
		</#if>
	</#list>
	</resultMap>

	<#assign iuColumnSize=0>
	<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
			<#assign iuColumnSize=iuColumnSize+1>
		</#if>
	</#list>

	<sql id="all_column_fields">
		 ${tableDO.allColumnString}
	</sql>
	
	<sql id="orders">
		<if test="orders != null and orders != ''">order by ${r'${orders}'}</if>
	</sql>
	
	<sql id="limits">
		<if test="dataLimit != null">limit ${r'${dataLimit}'}</if>
	</sql>
	
	<!-- xml转义字符需要 <![CDATA[   ]]> 标签-->
	<sql id="dynamic_where_fields">
		<where>
	<#list tableDO.columnList as col>
		<#assign aa="{${col.propertyName}}">
		<#if col.name!=tableDO.primaryKey && col.propertyType!="Date">
		<#if col.propertyType=="String">
			<if test="${col.propertyName} != null and ${col.propertyName} != ''"> AND ${col.name}=#${aa} </if>
		<#else>
			<if test="${col.propertyName} != null"> AND ${col.name}=#${aa} </if>
		</#if>
	</#if>
	</#list>
		</where>
	</sql>
	
	<delete id="deleteByPrimaryKey" parameterType="${tableDO.primaryColumnType}">
		DELETE FROM ${tableDO.tableName} WHERE ${tableDO.primaryKey} = #${primaryProperty}
	</delete>
	
	<insert id="insert" parameterType="${complete_package_class_name}" useGeneratedKeys="true" keyProperty="${tableDO.primaryProperty}" >
		<selectKey resultType="${tableDO.primaryColumnType}" keyProperty="${tableDO.primaryProperty}" order="AFTER">
			SELECT LAST_INSERT_ID() AS ${tableDO.primaryKey}
		</selectKey>
		INSERT INTO ${tableDO.tableName}(
		<#assign j=0>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign j=j+1>
		<#assign aa="{${col.name}}">
		<#if iuColumnSize!=j>
			${col.name},
		<#else>
			${col.name}
		</#if>
		</#if>
		</#list>
		)values(
		<#assign i=0>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign i=i+1>
		<#assign aa="{${col.propertyName}}">
		<#if iuColumnSize!=i>
			#${aa},
		<#else>
			#${aa}
		</#if>
		</#if>
		</#list>
		)
	</insert>
	
	<update id="updateByPrimaryKey" parameterType="${complete_package_class_name}">
		UPDATE ${tableDO.tableName}
		SET
		<#assign k=0>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign k=k+1>
		<#assign aa="{${col.propertyName}}">
		<#--><#if col.name!=tableDO.primaryKey>-->
		<#if col.name!=tableDO.primaryKey && col.name!=tableDO.createDateFieldName>
		<#if iuColumnSize!=k>
			${col.name} = #${aa},
		<#else>
			${col.name} = #${aa}
		</#if>
		</#if>
		</#if>
		</#list>
		WHERE
			${tableDO.primaryKey} = #${primaryProperty}
	</update>
	
	<select id="selectByPrimaryKey" parameterType="${tableDO.primaryColumnType}" resultMap="${resultMap}">
		SELECT
			<include refid="all_column_fields"/>
		FROM
			${tableDO.tableName}
		WHERE
			${tableDO.primaryKey} = #${primaryProperty}
	</select>
	
	<update id="updateDynamic" parameterType="${complete_package_class_name}">
		UPDATE ${tableDO.tableName}
		<set>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign aa="{${col.propertyName}}">
		<#--<#if col.name!=tableDO.primaryKey>-->
		<#if col.name!=tableDO.primaryKey && col.name!=tableDO.createDateFieldName>
		<#if col.propertyType=="String">
			<if test="${col.propertyName} != null and ${col.propertyName} != ''">${col.name} = #${aa},</if>
		<#else>
			<if test="${col.propertyName} != null">${col.name} = #${aa},</if>
		</#if>
		</#if>
		</#if>
		</#list>
		</set>
		WHERE ${tableDO.primaryKey} = #${primaryProperty}
	</update>
	
	<select id="selectDynamic" parameterType="${complete_package_class_name}" resultMap="${resultMap}">
		SELECT
			<include refid="all_column_fields"/>
		FROM
			${tableDO.tableName}
			<include refid="dynamic_where_fields"/>
			<include refid="orders"/>
			<include refid="limits"/>
	</select>

	<select id="selectCountDynamic" resultType="long" parameterType="${complete_package_class_name}">
		SELECT
			count(1)
		FROM
			${tableDO.tableName}
		<include refid="dynamic_where_fields"/>
	</select>

	<select id="selectDynamicPageQuery" parameterType="${complete_package_class_name}" resultMap="${resultMap}">
		SELECT
			<include refid="all_column_fields"/>
		FROM
			${tableDO.tableName}
		<include refid="dynamic_where_fields"/>
		<include refid="orders"/>
		Limit #${tableDO.pageStartFieldName},#${tableDO.pageSizeFieldName}
	</select>
	
	<insert id="saveForBatch" useGeneratedKeys="true" parameterType="java.util.List">
		<selectKey resultType="${tableDO.primaryColumnType}" keyProperty="id" order="AFTER">
			SELECT
			LAST_INSERT_ID() AS id
		</selectKey>
		INSERT INTO ${tableDO.tableName}(
		<#assign k=0>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign k=k+1>
		<#assign aa="${col.name}">
		<#if iuColumnSize != k>
			${aa},
		<#else>
			${aa}
		</#if>
		</#if>
		</#list>
		)values
		<foreach collection="list" item="item" index="index" separator="," >
		(
		<#assign f=0>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign f=f+1>
		<#assign aa="{item.${col.propertyName}}">
		<#if iuColumnSize != f>
			#${aa},
		<#else>
			#${aa}
		</#if>
		</#if>
		</#list>
		)
		</foreach>
	</insert>
	
	<update id="updateForBatch" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" separator=";">
			UPDATE ${tableDO.tableName}
		<set>
		<#list tableDO.columnList as col>
		<#if col.name != 'atime' && col.name != 'mtime'>
		<#assign aa="{item.${col.propertyName}}">
		<#assign primaryKey="{item.${tableDO.primaryKey}}">
		<#if col.name!=tableDO.primaryKey && col.propertyType!="Date">
			<#if col.propertyType=="String">
			<if test="item.${col.propertyName} != null and item.${col.propertyName} != ''"> ${col.name} = #${aa},</if>
			<#else>
			<if test="item.${col.propertyName} != null"> ${col.name} = #${aa}, </if>
			</#if>
		</#if>
		</#if>
		</#list>
		</set>
		WHERE ${tableDO.primaryKey} = #${primaryKey}
		</foreach>
	</update>
	
	<select id="selectOne" parameterType="${complete_package_class_name}" resultMap="${resultMap}">
		SELECT
			<include refid="all_column_fields"/>
		FROM
			${tableDO.tableName}
			<include refid="dynamic_where_fields"/>
			<include refid="orders"/>
			limit 1
	</select>
</mapper>